<?php
namespace home\controllers;

use home\models\Cart;
use home\models\CartForm;
use home\models\OrderProducts;
use home\models\Orders;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Json;
use yii\web\Response;

/**
 * Class CartController
 *
 * @package home\controllers
 */
class CartController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'add' => ['POST'],
                    'checkout' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * @return string
     */
    public function actionIndex()
    {
        $data = Cart::find()->where(['user_id' => Yii::$app->user->id])->all();

        return $this->render('index', [
            'data' => $data,
        ]);
    }

    public function actionConfirm()
    {
        $ids = Yii::$app->request->post('ids');

        $data = Cart::find()->where(['user_id' => Yii::$app->user->id, 'id' => $ids])->all();

        return $this->render('confirm', [
            'data' => $data,
        ]);
    }

    public function actionOrder()
    {
        $ids = Yii::$app->request->post('cart_ids');
        $name = Yii::$app->request->post('name');
        $area_id = Yii::$app->request->post('area_id');
        $address = Yii::$app->request->post('address');
        $memo = Yii::$app->request->post('memo');

        $data = Cart::find()->where(['user_id' => Yii::$app->user->id, 'id' => $ids])->all();

        $order = new Orders();
        $order->id = date('YmdHid') . rand(10000, 99999);
        $order->user_id = Yii::$app->user->id;
        $order->name = $name;
        $order->phone = Yii::$app->user->identity->mobile;
        $order->area_id = $area_id;
        $order->address = $address;
        $order->memo = $memo;
        $order->save();

        /* @var Cart[] $data */
        foreach($data as $datum){
            $product = new OrderProducts();
            $product->order_id = $order->id;
            $product->product_id = $datum->product_id;
            $product->quantity = $datum->quantity;
            $product->data = $datum->data;
            if($product->save()){
                $datum->delete();
            }
        }

        Yii::$app->session->setFlash('success', '订单提交成功！请等待客服人员联系！');

        return $this->redirect(['site/index']);
    }

    public function actionAdd()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;

        $model = new CartForm();

        if($model->load(Yii::$app->request->post(), 'Cart') && $model->save()){
            return [
                'error' => 0,
                'msg' => '成功添加到购物车！',
                'quantity' => Cart::getCount(),
            ];
        }

        return [
            'error' => 1,
            'msg' => '添加失败！',
        ];
    }

    public function actionUpdate()
    {
        $id = Yii::$app->request->post('id');
        $quantity = Yii::$app->request->post('quantity');

        Cart::updateAll(['quantity' => $quantity], ['id' => $id, 'user_id' => Yii::$app->user->id]);
    }

    public function actionRemove()
    {
        $ids = Yii::$app->request->get('ids');

        if(Yii::$app->request->getIsPost()){
            $ids = Yii::$app->request->post('ids');
        }

        Cart::removeAll($ids);

        if(Yii::$app->request->getIsPost()){
            return Json::encode(['error' => 0]);
        }

        return $this->redirect(['index']);
    }
}
